package revolucion.window;

import revolucion.classes.User;
import revolucion.classes.Log;
import revolucion.database.Database;

import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

public class Window_Login extends JFrame {
  public Window_Login() {
    super( ".:: Login" );

    initComponents();

    setIconImage(new ImageIcon(getClass().getResource("/revolucion/resources/admin.gif")).getImage());

    Dimension dim = this.getToolkit().getScreenSize();
    int x = (int) (dim.getWidth()  - this.getSize().getWidth() ) / 2;
    int y = (int) (dim.getHeight() - this.getSize().getHeight()) / 2;
    this.setLocation(x,y);
  }
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    txt_user = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    btn_login = new javax.swing.JButton();
    txt_pass = new javax.swing.JPasswordField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBounds(new java.awt.Rectangle(0, 0, 0, 0));
    setIconImages(null);
    setName("wnd_login"); // NOI18N
    setResizable(false);

    txt_user.setName("txt_user"); // NOI18N
    txt_user.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        txt_userKeyPressed(evt);
      }
    });

    jLabel1.setText("Usuário:");
    jLabel1.setName("jLabel1"); // NOI18N

    jLabel2.setText("Senha:");
    jLabel2.setName("jLabel2"); // NOI18N

    btn_login.setText("Entrar");
    btn_login.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    btn_login.setFocusable(false);
    btn_login.setName("btn_login"); // NOI18N
    btn_login.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btn_loginActionPerformed(evt);
      }
    });

    txt_pass.setName("txt_pass"); // NOI18N
    txt_pass.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        txt_passKeyPressed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jLabel1)
          .addComponent(jLabel2))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(txt_pass)
          .addComponent(txt_user, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btn_login, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(btn_login, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(jLabel1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(jLabel2)
              .addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_loginActionPerformed
    Database.getConnection();
    boolean exist = Database.checkUser( txt_user.getText(), txt_pass.getText() );
    if(exist) {
      User user = new User();
      Window_Main w = new Window_Main(user.loadUser(txt_user.getText()));

      this.dispose();
      w.setVisible(true);
      Log.writeLog(user.getUsername(), "Usuário conectado!", "OK");
    } else {
      JOptionPane.showMessageDialog( this, "Erro ao conectar!\nVerifique seus dados ou contate o administrador do sistema." );
      Log.writeLog("<nenhum>", "Erro ao conectar com usuário " + txt_user.getText(), "");
    }
  }//GEN-LAST:event_btn_loginActionPerformed

  private void txt_passKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_passKeyPressed
    if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
      btn_login.doClick();
    }
  }//GEN-LAST:event_txt_passKeyPressed

  private void txt_userKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_userKeyPressed
    if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
      btn_login.doClick();
    }
  }//GEN-LAST:event_txt_userKeyPressed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btn_login;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  public javax.swing.JPasswordField txt_pass;
  public javax.swing.JTextField txt_user;
  // End of variables declaration//GEN-END:variables
}
